package com.bwie.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.user.entity.ActivityEntity;
import com.bwie.user.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.user.dao.GoodsDao;
import com.bwie.user.entity.GoodsEntity;
import com.bwie.user.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RedissonClient client;

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
//        String key = (String) params.get("key");
//        if (StringUtils.isNotEmpty(key)){
//            wrapper.and(w->{
//               w.like("goods_name",key).or().lt("goods_stock",key);
//            });
//        }
//        IPage<GoodsEntity> page = this.page(
//                    new Query<GoodsEntity>().getPage(params),
//                    wrapper
//           );
//        return new PageUtils(page);
//    }
    /**
     * redis缓存 缓存商品页面第一页
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        String page1 = (String) params.get("page");
        //先去看redis里有没有
        IPage<GoodsEntity> goodsList = (IPage<GoodsEntity>) redisTemplate.boundHashOps("goods").get("list");
            //先判断是首页并且redis为null 并且key为null 就去查询数据库并存
            if (goodsList==null&&Integer.parseInt(page1)==1&&StringUtils.isEmpty(key)){
                IPage<GoodsEntity> page = this.page(
                        new Query<GoodsEntity>().getPage(params),
                        new QueryWrapper<>()
                );
                System.out.println("查询数据库并缓存第一页");
                redisTemplate.boundHashOps("goods").put("list",page);
                return new PageUtils(page);
                //else if（再去判断是首页key为null 并且redis有数据）这个就去查询redis缓存
            }else if (goodsList!=null&&Integer.parseInt(page1)==1&&StringUtils.isEmpty(key)){

                System.out.println("===============缓存命中");
                return  new PageUtils(goodsList);

            }else { //else(  去直接查数据库 )
                System.out.println("直接去数据库查goodsList");
                IPage<GoodsEntity> page = this.page(
                        new Query<GoodsEntity>().getPage(params),
                        new QueryWrapper<GoodsEntity>().like("goods_name",key).or().lt("goods_stock",key)
                );
                return new PageUtils(page);
            }



    }

    @Override
    public PageUtils queryPage2(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)){
            wrapper.and(w->{
                w.eq("gid",key);
            });
        }
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }


}